# -*- coding: utf-8 -*-
# @Time    : 2019/10/8 15:04
# @Author  : LiHaowen
# @Email   : lihaowen2017@163.com
# @File    : TornadoServer.py
# django的wsgi放到Tornado的wsgi中的多个端口实现同步框架的高性能部署
import os
import sys
from tornado.options import options, define  # 类似于导入django模型类视图一样
from django.core.wsgi import get_wsgi_application  # django框架实现的wsgi服务放到tornado wsgi 服务
import tornado.httpserver
import tornado.ioloop  # 事件循环
import tornado.web
import tornado.wsgi

# 查找应用的路径
app_path = os.path.abspath(
    os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
)
sys.path.append(os.path.join(app_path, "web_ask"))  # ..web_ask/web_ask
# 定义tornado端口，端口其实时启动脚本时指定的
define("port", default=6000, type=int, help="run on the given port")


def main():
    tornado.options.parse_command_line()  #
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")  # 定义配置文件的路径
    wsgi_app = tornado.wsgi.WSGIContainer(get_wsgi_application())  # 定义tornado的wsgi应用
    http_server = tornado.httpserver.HTTPServer(wsgi_app, xheaders=True)  # xheaders=True 后端才能接受到客户端的ip
    http_server.listen(options.port)  # http监听改端口
    tornado.ioloop.IOLoop.instance().start()  # 启动该实例


if __name__ == '__main__':
    main()
